package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.FodderStorageDAO;
import cn.edu.scau.cmi.hombio.dao.FodderTypeDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.FodderStorage;
import cn.edu.scau.cmi.hombio.domain.FodderType;

import cn.edu.scau.cmi.hombio.service.FodderStorageService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for FodderStorage entities
 * 
 */

@Controller("FodderStorageRestController")

public class FodderStorageRestController {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages FodderStorage entities
	 * 
	 */
	@Autowired
	private FodderStorageDAO fodderStorageDAO;

	/**
	 * DAO injected by Spring that manages FodderType entities
	 * 
	 */
	@Autowired
	private FodderTypeDAO fodderTypeDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for FodderStorage entities
	 * 
	 */
	@Autowired
	private FodderStorageService fodderStorageService;

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@RequestMapping(value = "/FodderStorage/{fodderstorage_id}/employee/{employee_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteFodderStorageEmployee(@PathVariable Integer fodderstorage_id, @PathVariable Integer related_employee_id) {
		fodderStorageService.deleteFodderStorageEmployee(fodderstorage_id, related_employee_id);
	}

	/**
	* Select an existing FodderStorage entity
	* 
	*/
	@RequestMapping(value = "/FodderStorage/{fodderstorage_id}", method = RequestMethod.GET)
	@ResponseBody
	public FodderStorage loadFodderStorage(@PathVariable Integer fodderstorage_id) {
		return fodderStorageDAO.findFodderStorageByPrimaryKey(fodderstorage_id);
	}

	/**
	* View an existing FodderType entity
	* 
	*/
	@RequestMapping(value = "/FodderStorage/{fodderstorage_id}/fodderType/{foddertype_id}", method = RequestMethod.GET)
	@ResponseBody
	public FodderType loadFodderStorageFodderType(@PathVariable Integer fodderstorage_id, @PathVariable Integer related_foddertype_id) {
		FodderType foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(related_foddertype_id, -1, -1);

		return foddertype;
	}

	/**
	* Delete an existing FodderType entity
	* 
	*/
	@RequestMapping(value = "/FodderStorage/{fodderstorage_id}/fodderType/{foddertype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteFodderStorageFodderType(@PathVariable Integer fodderstorage_id, @PathVariable Integer related_foddertype_id) {
		fodderStorageService.deleteFodderStorageFodderType(fodderstorage_id, related_foddertype_id);
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Create a new Employee entity
	* 
	*/
	@RequestMapping(value = "/FodderStorage/{fodderstorage_id}/employee", method = RequestMethod.POST)
	@ResponseBody
	public Employee newFodderStorageEmployee(@PathVariable Integer fodderstorage_id, @RequestBody Employee employee) {
		fodderStorageService.saveFodderStorageEmployee(fodderstorage_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Delete an existing FodderStorage entity
	* 
	*/
	@RequestMapping(value = "/FodderStorage/{fodderstorage_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteFodderStorage(@PathVariable Integer fodderstorage_id) {
		FodderStorage fodderstorage = fodderStorageDAO.findFodderStorageByPrimaryKey(fodderstorage_id);
		fodderStorageService.deleteFodderStorage(fodderstorage);
	}

	/**
	* Get Employee entity by FodderStorage
	* 
	*/
	@RequestMapping(value = "/FodderStorage/{fodderstorage_id}/employee", method = RequestMethod.GET)
	@ResponseBody
	public Employee getFodderStorageEmployee(@PathVariable Integer fodderstorage_id) {
		return fodderStorageDAO.findFodderStorageByPrimaryKey(fodderstorage_id).getEmployee();
	}

	/**
	* Save an existing FodderStorage entity
	* 
	*/
	@RequestMapping(value = "/FodderStorage", method = RequestMethod.PUT)
	@ResponseBody
	public FodderStorage saveFodderStorage(@RequestBody FodderStorage fodderstorage) {
		fodderStorageService.saveFodderStorage(fodderstorage);
		return fodderStorageDAO.findFodderStorageByPrimaryKey(fodderstorage.getId());
	}

	/**
	* Save an existing FodderType entity
	* 
	*/
	@RequestMapping(value = "/FodderStorage/{fodderstorage_id}/fodderType", method = RequestMethod.PUT)
	@ResponseBody
	public FodderType saveFodderStorageFodderType(@PathVariable Integer fodderstorage_id, @RequestBody FodderType foddertype) {
		fodderStorageService.saveFodderStorageFodderType(fodderstorage_id, foddertype);
		return fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype.getId());
	}

	/**
	* Get FodderType entity by FodderStorage
	* 
	*/
	@RequestMapping(value = "/FodderStorage/{fodderstorage_id}/fodderType", method = RequestMethod.GET)
	@ResponseBody
	public FodderType getFodderStorageFodderType(@PathVariable Integer fodderstorage_id) {
		return fodderStorageDAO.findFodderStorageByPrimaryKey(fodderstorage_id).getFodderType();
	}

	/**
	* Create a new FodderStorage entity
	* 
	*/
	@RequestMapping(value = "/FodderStorage", method = RequestMethod.POST)
	@ResponseBody
	public FodderStorage newFodderStorage(@RequestBody FodderStorage fodderstorage) {
		fodderStorageService.saveFodderStorage(fodderstorage);
		return fodderStorageDAO.findFodderStorageByPrimaryKey(fodderstorage.getId());
	}

	/**
	* Save an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/FodderStorage/{fodderstorage_id}/employee", method = RequestMethod.PUT)
	@ResponseBody
	public Employee saveFodderStorageEmployee(@PathVariable Integer fodderstorage_id, @RequestBody Employee employee) {
		fodderStorageService.saveFodderStorageEmployee(fodderstorage_id, employee);
		return employeeDAO.findEmployeeByPrimaryKey(employee.getId());
	}

	/**
	* Create a new FodderType entity
	* 
	*/
	@RequestMapping(value = "/FodderStorage/{fodderstorage_id}/fodderType", method = RequestMethod.POST)
	@ResponseBody
	public FodderType newFodderStorageFodderType(@PathVariable Integer fodderstorage_id, @RequestBody FodderType foddertype) {
		fodderStorageService.saveFodderStorageFodderType(fodderstorage_id, foddertype);
		return fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype.getId());
	}

	/**
	* View an existing Employee entity
	* 
	*/
	@RequestMapping(value = "/FodderStorage/{fodderstorage_id}/employee/{employee_id}", method = RequestMethod.GET)
	@ResponseBody
	public Employee loadFodderStorageEmployee(@PathVariable Integer fodderstorage_id, @PathVariable Integer related_employee_id) {
		Employee employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		return employee;
	}

	/**
	* Show all FodderStorage entities
	* 
	*/
	@RequestMapping(value = "/FodderStorage", method = RequestMethod.GET)
	@ResponseBody
	public List<FodderStorage> listFodderStorages() {
		return new java.util.ArrayList<FodderStorage>(fodderStorageService.loadFodderStorages());
	}
}